<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>简单API测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
        }
        .container {
            background-color: #f5f5f5;
            padding: 20px;
            border-radius: 8px;
        }
        h1 {
            text-align: center;
            color: #333;
        }
        button {
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background-color: #45a049;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            background-color: white;
            border-radius: 4px;
            font-family: monospace;
            white-space: pre-wrap;
        }
        .success {
            color: #155724;
            background-color: #d4edda;
            border: 1px solid #c3e6cb;
        }
        .error {
            color: #721c24;
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
        }
        .loading {
            color: #0c5460;
            background-color: #d1ecf1;
            border: 1px solid #bee5eb;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>简单API测试</h1>
        
        <h3>1. 测试后端连接</h3>
        <button id="testConnectionBtn">测试连接</button>
        
        <h3>2. 测试登录API</h3>
        <button id="testLoginBtn">测试登录</button>
        
        <div id="result" class="result">点击上方按钮开始测试...</div>
    </div>

    <script>
        const resultElement = document.getElementById('result');
        const API_BASE_URL = 'http://localhost:8080';
        
        function showLoading(message) {
            resultElement.textContent = message;
            resultElement.className = 'result loading';
        }
        
        function showSuccess(message) {
            resultElement.textContent = message;
            resultElement.className = 'result success';
        }
        
        function showError(message) {
            resultElement.textContent = message;
            resultElement.className = 'result error';
        }
        
        // 测试后端连接
        document.getElementById('testConnectionBtn').addEventListener('click', async function() {
            showLoading('正在测试后端连接...');
            
            try {
                const startTime = Date.now();
                const response = await fetch(API_BASE_URL + '/api/system/health', {
                    method: 'GET',
                    timeout: 5000
                });
                const endTime = Date.now();
                
                if (response.ok) {
                    showSuccess(`后端连接成功！\n响应状态码: ${response.status}\n响应时间: ${endTime - startTime}ms\n服务器时间: ${new Date().toLocaleString()}`);
                } else {
                    showError(`后端连接失败！\n状态码: ${response.status}\n响应时间: ${endTime - startTime}ms`);
                }
            } catch (error) {
                showError(`连接出错: ${error.message}\n请检查后端服务是否正在运行`);
            }
        });
        
        // 测试登录API
        document.getElementById('testLoginBtn').addEventListener('click', async function() {
            showLoading('正在测试登录API...');
            
            try {
                const startTime = Date.now();
                const response = await fetch(API_BASE_URL + '/api/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        username: 'admin',
                        password: 'admin123'
                    })
                });
                const endTime = Date.now();
                
                const data = await response.json();
                
                if (response.ok && data.code === 200 && data.data) {
                    showSuccess(`登录成功！\n响应状态码: ${response.status}\n响应时间: ${endTime - startTime}ms\nToken长度: ${data.data.length}字符\nToken前20位: ${data.data.substring(0, 20)}...`);
                } else {
                    showError(`登录失败！\n状态码: ${response.status}\n错误信息: ${data.msg || '未知错误'}\n响应时间: ${endTime - startTime}ms`);
                }
            } catch (error) {
                showError(`登录请求出错: ${error.message}\n请检查后端服务是否正在运行`);
            }
        });
        
        // 页面加载时自动测试连接
        window.onload = function() {
            document.getElementById('testConnectionBtn').click();
        };
    </script>
</body>
</html>